package lew.bing.exp.spr;

import lew.bing.exp.spr.filters.TestFilter;
import lew.bing.exp.spr.filters.WebBasicAuthFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;

import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;

import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;

/**
 * Created by 刘国兵 on 2018/7/10.
 */
@SpringBootApplication
@ServletComponentScan
public class Bootstrap extends WebMvcConfigurationSupport {

//    @Bean
//    public RouterFunction<ServerResponse> router() {
//        return route(GET("/"), new HandlerFunction<ServerResponse>() {
//            @Override
//            public Mono<ServerResponse> handle(ServerRequest request) {
//                return ServerResponse.ok().body(fromObject("hello"));
//            }
//        });
//    }
//
//    public Flux<ServerSentEvent<Integer>> randomNumbers() {
//
//
//
//        return Flux.interval(Duration.ofSeconds(1L))
//                .map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
//                .map(data -> ServerSentEvent.<Integer>builder().event("random").id(Long.toString(data.getT1())).data(data.getT2()).build());
//    }

    private static final Logger logger = LoggerFactory.getLogger(Bootstrap.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Bootstrap.class, args);
    }

//    @Bean
    public FilterRegistrationBean<TestFilter> testFilter() {
        FilterRegistrationBean<TestFilter> testFilterFilterRegistrationBean = new FilterRegistrationBean<>(new TestFilter());
        testFilterFilterRegistrationBean.addUrlPatterns("/*");
        logger.info("registerBean => {}",testFilterFilterRegistrationBean);
        return testFilterFilterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<WebBasicAuthFilter> webBasicAuthFilterFilterRegistrationBean(WebBasicAuthFilter webBasicAuthFilter) {
        FilterRegistrationBean<WebBasicAuthFilter> webBasicAuthFilterFilterRegistrationBean = new FilterRegistrationBean<>(webBasicAuthFilter);
        webBasicAuthFilterFilterRegistrationBean.addInitParameter("username","admin");
        webBasicAuthFilterFilterRegistrationBean.addInitParameter("pwd","admin");
        webBasicAuthFilterFilterRegistrationBean.addUrlPatterns("/*");
        return webBasicAuthFilterFilterRegistrationBean;
    }

    @Bean
    public ServletRegistrationBean<DispatcherServlet> dispatcherServletServletRegistrationBean(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean<DispatcherServlet> registrationBean = new ServletRegistrationBean<>(dispatcherServlet);
        registrationBean.setName("myDispatcherServlet");
        logger.info("dispatchServlet now name is => {}",registrationBean.getServletName());
        return registrationBean;
    }
}
